/**
 * Company : Thai Informatic System Co., Ltd.
 * Created By : Dr.Tree
 * Created On : Feb 13, 2012  12:40:36 PM 
 * e-Mail : chatree@tis.co.th
 * Mobile Phone : (+66) 087-921-5115
 * Package Name : sap.business.mm.inbound
 * File Name : MmPlantInbService.java
 * TODO
 */
package sap.business.mm.inbound;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;

import jpa.entitys.synchro.common.ACTION_HANDLER;
import jpa.entitys.synchro.staging.MmPlantInb;
import jpa.service.common.SessionEntityManager;
import jpa.service.synchro.staging.MmPlantInbServices;

import org.apache.log4j.Logger;

import promis.utility.CommonService;
import sap.business.commons.services.CommonJCOReturn;
import apps.promis.in.common.ProjectAction;

import com.sap.mw.jco.JCO;

/**
 * @author chatree
 *
 */
public class MmPlantInbService {
	
	private static final Logger log = Logger.getLogger(MmPlantInbService.class);
	
	public void CreatedSync(JCO.ParameterList input, JCO.ParameterList tables, JCO.ParameterList output, String structureOrTableName) throws Exception {
		
		List<MmPlantInb> lst_object = new ArrayList<MmPlantInb>();
		JCO.Table table = tables.getTable(structureOrTableName);
		List<MmPlantInb> lst = null;
		int records = -1;
		try{
			
			records = table.getNumRows();

			EntityManager em = new SessionEntityManager().CreatedEntityManager();
			MmPlantInbServices MmPlantInbServ = new MmPlantInbServices(em);
			String instance_no = MmPlantInbServ.GetNewInstanceNo();
			
			for(int i = 0; i < records; i++)
			{
				log.info("i ="+ i);
				table.setRow(i);			
				MmPlantInb model = new MmPlantInb();
				
				//model.setPromisStagingId(CommonService.IsNullRNull(table.getString("PROMIS_STAGING_ID")));
				model.setInstanceNo(instance_no);
				model.setPlantCode(CommonService.IsNullRNull(table.getString("PLANT_CODE")));				
				model.setPlantDescri(CommonService.IsNullRNull(table.getString("PLANT_DESCRIPTION")));
				model.setCompanyCode(CommonService.IsNullRNull(table.getString("COMPANY_CODE")));								
				model.setProjectCode(CommonService.IsNullRNull(table.getString("PROJECT_CODE")));	
				
				
				//Set Txn Reacord
				model.setCreatedOn(CommonService.IsNullRNull(table.getString("CREATED_ON")));
				model.setCreatedBy(CommonService.IsNullRNull(table.getString("CREATED_BY")));
				model.setUpdatedOn(CommonService.IsNullRNull(table.getString("LAST_UPD_ON")));
				model.setUpdatedBy(CommonService.IsNullRNull(table.getString("LAST_UPD_BY")));
				
				if(CommonService.IsNull(table.getString("LAST_UPD_ON")))
					model.setActionHandle(ACTION_HANDLER.INSERTED.toString());
				else 
					model.setActionHandle(ACTION_HANDLER.UPDATED.toString());
				
				//Set SAP External Key link
				model.setExtRefKey(CommonService.IsNullRNull(table.getString("SYNC_EXTERNAL_REF_KEY")));
				model.setExtRefNo(CommonService.IsNullRNull(table.getString("SYNC_EXTERNAL_REF_NO")));
				
				log.info(model.toString());
				lst_object.add(model);				
			}
			
			//Create To Staging
			MmPlantInbServ.Inserted(lst_object);
			log.info("Create To Staging  ----------------------------------->");
			
			//find to Staging
			lst = MmPlantInbServ.findByInstanceNo(instance_no);
			log.info("find to Staging instance_no : "+ instance_no +"  ----------------------------------->");
			
			
			if(!CommonService.IsNull(lst))
			{
				//Create to Promis
				new ProjectAction().CreateToPromis(lst);
				log.info(" Create to Promis ----------------------------------->");
				
				if(lst.size() == records){
					for(int i = 0; i < records; i++)
					{
						MmPlantInb obj = lst.get(i);
						table.setRow(i);
						table.setValue(CommonService.IsNullRString(obj.getPromisStagingId()), "PROMIS_TXN_ID");
						table.setValue("S", "MSG_CODE");
						table.setValue("Success", "MSG_DESC");
						table.setValue("SAP NO =" + obj.getExtRefNo(), "MSG_ALT_DESC");
						table.setValue("", "MSG_ACTION_RESULT");	
						log.info(CommonJCOReturn.toString(table));
					}
				}
			}else
				log.info(" find not found  instance no = " + instance_no);
		}
		catch(Exception e)
		{	log.error(e);
			if(!CommonService.IsNull(lst)){
				//Create to Promis
					if(lst.size() == records){
						for(int i = 0; i < records; i++)
						{
							MmPlantInb obj = lst.get(i);
							table.setRow(i);
							table.setValue(CommonService.IsNullRString(obj.getPromisStagingId()), "PROMIS_TXN_ID");
							table.setValue("E", "MSG_CODE");
							table.setValue(e.getMessage(), "MSG_DESC");
							table.setValue("SAP NO =" + obj.getExtRefNo(), "MSG_ALT_DESC");
							table.setValue("", "MSG_ACTION_RESULT");
							
							try {
								log.info(CommonJCOReturn.toString(table));
							} catch (Exception e1) {
								log.error(e1.getMessage());
								System.err.println(e1.getMessage());
							}
						}
					}			
			}else{			
				for(int i = 0; i < records; i++)
				{
					table.setRow(i);
					table.setValue("E", "MSG_CODE");
					table.setValue(e.getMessage(), "MSG_DESC");
				}
			}
		}
	}

}
